<template>
  <div>
    <el-card>
      <el-button type="primary" @click="add">新增<i class="el-icon-plus el-icon--right" /></i></el-button>
      <el-table :data="tableData.list" style="width: 100%">
        <el-table-column align="center" prop="realName" label="姓名" />
        <el-table-column align="center" prop="phone" label="手机号" />
        <el-table-column align="center" prop="username" label="用户名" />
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="small" @click="edit(scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="removed(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--
          current-page:代表的是当前第几页
          total：代表分页器一共需要展示多少条数据
          page-size：代表每页默认展示数据条数
          page-sizes：代表可以设置每一页需要展示多少条数据
          layout：可以实现分页器布局
          @current-change:当页码发生变化触发
          @size-change：当每页展示条数发送变化触发
         -->
      <el-pagination :current-page="postForm.currentPage" :total="tableData.total" :page-size="postForm.pageSize" :page-sizes="[1, 3, 5, 10]" style="padding: 20px 0" layout="total, sizes, prev, pager, next, jumper" @current-change="handleCurrentChange" @size-change="handleSizeChange" />
    </el-card>
    <formDialog :close-modal="false" :title="dialog_form.title" :visible="dialog_form.visible" :data-source="dialog_form.form" :get-list="loadData" :disabled="dialog_form.disabled" @close="closeFormDialog" />
  </div>
</template>

<script>
import { sysUserList, submitSysUser } from '@/api/user.js'
import formDialog from './components/formDialog.vue'
export default {
  name: 'Dashboard',
  components: { formDialog },
  data() {
    return {
      // 搜索
      postForm: {
        currentPage: 1,
        pageSize: 10
      },
      // 表格
      tableData: {
        list: [],
        total: 0
      },
      // dialog框
      dialog_form: {
        visible: false,
        form: {},
        title: '',
        disabled: false
      },

      // 提交的数据
      initForm: {
        username: '',
        password: '',
        phone: '',
        realName: '',
        id: '',
        remove: false
      }
    }
  },
  mounted() {
    this.loadData(this.$filNull(this.postForm))
  },
  methods: {
    // 搜索
    search() {
      this.loadData(this.$filNull(this.postForm))
    },
    // 获取系统用户列表
    loadData(postForm) {
      sysUserList(postForm).then(res => {
        const { data, msg, result } = res
        if (result == 1) {
          this.tableData.list = data.list
          this.tableData.total = data.total
        } else {
          this.$message.warning(msg)
          this.tableData.list = []
        }
      })
    },
    // 页码点击事件handleCurrentChange
    handleCurrentChange(currentPage) {
      this.postForm.currentPage = currentPage
      this.loadData(this.postForm)
    },
    // 展示条数变化事件
    handleSizeChange(pageSize) {
      this.postForm.pageSize = pageSize
      this.loadData(this.postForm)
    },
    // 关闭dialog弹窗
    closeFormDialog() {
      this.dialog_form.visible = false
      // this.dialog_form.disabled = false
      this.initForm = {}
    },
    // 新增
    add() {
      this.dialog_form.title = '新增'
      this.dialog_form.visible = true
      // this.dialog_form.disabled = true
      this.dialog_form.form = this.initForm
    },
    // 编辑
    edit(row) {
      this.dialog_form.title = '编辑'
      this.dialog_form.visible = true
      // row.password = ''
      this.dialog_form.form = row
      console.log(this.dialog_form.form)
    },
    // 删除
    removed(row) {
      this.initForm.id = row.id
      this.initForm.removed = true
      this.$confirm('此操作将永久删除该用户 , 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          submitSysUser(this.$filNull(this.initForm)).then(res => {
            const { result, msg, data } = res
            if (result == 1) {
              this.$message.success(msg)
              this.loadData(this.postForm)
            } else {
              this.$message.warning(msg)
              this.loadData(this.postForm)
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
.image[data-v-e7954e76] {
  // display: block;
  width: 60px;
  height: 60px;
}
</style>
